$(function() {

    var deUnicode= function (v) {
        var r = v.match(/\\u[0-9a-fA-F]{4}/g);
        if (r == null) {
            return v;
        }
        for (var i = 0; i < r.length; i++) {
            v = v.replace(r[i], unescape(r[i].replace("\\u", "%u")));
        }
        return v;
    }

    // alarmConfig
    $('.alarmConfig').on('click', function(){
        var taskName = $(this).attr('taskName');
        var taskType = $(this).attr('taskType');
        window.location.href = base_url + "/alarm?taskName=" + taskName+"&taskType="+taskType;
    });

	// remove
	$('.remove').on('click', function(){
		var taskName = $(this).attr('taskName');
        var taskType = $(this).attr('taskType');

        layer.confirm( "确认删除该任务定义?" , {
            icon: 3,
            title: '系统提示' ,
            btn: [ '确定', '取消' ]
        }, function(index){
            layer.close(index);

            $.ajax({
                type : 'POST',
                url : base_url + '/task/remove',
                data : {"taskName":taskName,"taskType":taskType},
                dataType : "json",
                success : function(data){
                    if (data.status == 200) {
                        layer.open({
                            icon: '1',
                            content: '删除成功' ,
                            end: function(layero, index){
                                window.location.reload();
                            }
                        });

                    } else {
                        layer.open({
                            icon: '2',
                            content: (data.message||'删除失败')
                        });
                    }
                },
            });

        });

	});

    // immediately_exec
    $('.immediately_exec').on('click', function(){
        var taskName = $(this).attr('taskName');
        var taskType = $(this).attr('taskType');

        layer.confirm( "确认立即执行该任务?" , {
            icon: 3,
            title: '系统提示' ,
            btn: [ '确定', '取消' ]
        }, function(index){
            layer.close(index);

            $.ajax({
                type : 'POST',
                url : base_url + '/task/immediately-exec',
                data : {"taskName":taskName,"taskType":taskType},
                dataType : "json",
                success : function(data){
                    if (data.status == 200) {
                        layer.open({
                            icon: '1',
                            content: '操作成功' ,
                            end: function(layero, index){
                                window.location.reload();
                            }
                        });

                    } else {
                        layer.open({
                            icon: '2',
                            content: (data.message||'操作失败')
                        });
                    }
                },
            });

        });

    });




	// jquery.validate 自定义校验
	jQuery.validator.addMethod("validEnv", function(value, element) {
		var length = value.length;
		var valid = /^[a-z]*$/;
		return this.optional(element) || valid.test(value);
	}, "限制由小写字母组成");

	$('.add').on('click', function(){
		$('#addModal').modal({backdrop: false, keyboard: false}).modal('show');

        $("#div_http_addr").attr("style","display:none;");
        $('#div_http_method').attr("style","display:none;");

        $("#div_dubbo_service").attr("style","display:block;");
        $('#div_dubbo_method').attr("style","display:block;");
        $("#div_dubbo_version").attr("style","display:block;");
        $('#div_dubbo_group').attr("style","display:block;");
	});

    $('#taskType').change(function(){
       // alert($(this).children('option:selected').val());
        var p1=$(this).children('option:selected').val();
        if(p1==1){
            $("#div_http_addr").attr("style","display:none;");
            $('#div_http_method').attr("style","display:none;");

            $("#div_dubbo_service").attr("style","display:block;");
            $('#div_dubbo_method').attr("style","display:block;");
            $("#div_dubbo_version").attr("style","display:block;");
            $('#div_dubbo_group').attr("style","display:block;");
        }else {
            $("#div_http_addr").attr("style","display:block;");
            $('#div_http_method').attr("style","display:block;");

            $("#div_dubbo_service").attr("style","display:none;");
            $('#div_dubbo_method').attr("style","display:none;");
            $("#div_dubbo_version").attr("style","display:none;");
            $('#div_dubbo_group').attr("style","display:none;");
        }

    })


    var addModalValidate = $("#addModal .form").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
		rules : {
            taskName : {
				required : true,
				rangelength:[3,50],
			},
            cronExp : {
				required : true,
				rangelength:[1, 100]
			},
            owner : {
                required : true,
                rangelength:[1, 100]
            },
            ownerPhone : {
                required : true,
                rangelength:[4, 100]
            },
            ownerEmail : {
                required : true,
                rangelength:[4, 100]
            },
            order : {
                digits:true
            }
		},
		messages : {
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {
			$.post(base_url + "/task/save",  $("#addModal .form").serialize(), function(data, status) {
                if (data.status == 200) {
                    $('#addModal').modal('hide');

                    layer.open({
                        icon: '1',
                        content: '新增成功' ,
                        end: function(layero, index){
                            window.location.reload();
                        }
                    });

                } else {
                    layer.open({
                        icon: '2',
                        content: (data.message||'新增失败')
                    });
                }
			});
		}
	});
	$("#addModal").on('hide.bs.modal', function () {
		$("#addModal .form")[0].reset();
		addModalValidate.resetForm();
		$("#addModal .form .form-group").removeClass("has-error");
	});

	$('.update').on('click', function(){
		$("#updateModal .form input[name='taskName']").val($(this).attr("taskName"));
		$("#updateModal .form input[name='taskType']").val($(this).attr("taskType"));
        $("#updateModal .form input[name='cronExp']").val($(this).attr("cronExp"));
        $("#updateModal .form input[name='status']").val($(this).attr("status"));
        $("#updateModal .form input[name='concurrentModel']").val($(this).attr("concurrentModel"));
        $("#updateModal .form input[name='scheduleNode']").val($(this).attr("scheduleNode"));
        $("#updateModal .form input[name='executorRouteStrategy']").val($(this).attr("executorRouteStrategy"));

        $("#updateModal .form textarea[name='executorParam']").val($(this).attr("executorParam"));

        $("#updateModal .form input[name='owner']").val($(this).attr("owner"));
        $("#updateModal .form input[name='ownerPhone']").val($(this).attr("ownerPhone"));
        $("#updateModal .form input[name='ownerEmail']").val($(this).attr("ownerEmail"));
        $("#updateModal .form input[name='executorFailRetryCount']").val($(this).attr("executorFailRetryCount"));
        $("#updateModal .form input[name='executorTimeout']").val($(this).attr("executorTimeout"));

        $("#updateModal .form input[name='dubboService']").val($(this).attr("dubboService"));
        $("#updateModal .form input[name='dubboMethod']").val($(this).attr("dubboMethod"));
        $("#updateModal .form input[name='dubboGroup']").val($(this).attr("dubboGroup"));
        $("#updateModal .form input[name='dubboVersion']").val($(this).attr("dubboVersion"));

        $("#updateModal .form input[name='httpUrl']").val($(this).attr("httpUrl"));
        $("#updateModal .form input[name='httpMethod']").val($(this).attr("httpMethod"));

		$('#updateModal').modal({backdrop: false, keyboard: false}).modal('show');

        var  p1=$(this).attr("taskType");
        $("#upt_taskType").find("option[value="+p1+"]").prop("selected",true);
        if(p1==1){
            $("#div_upt_http_addr").attr("style","display:none;");
            $('#div_upt_http_method').attr("style","display:none;");

            $("#div_upt_dubbo_service").attr("style","display:block;");
            $('#div_upt_dubbo_method').attr("style","display:block;");
            $("#div_upt_dubbo_version").attr("style","display:block;");
            $('#div_upt_dubbo_group').attr("style","display:block;");
        }else {
            $("#div_upt_http_addr").attr("style","display:block;");
            $('#div_upt_http_method').attr("style","display:block;");

            $("#div_upt_dubbo_service").attr("style","display:none;");
            $('#div_upt_dubbo_method').attr("style","display:none;");
            $("#div_upt_dubbo_version").attr("style","display:none;");
            $('#div_upt_dubbo_group').attr("style","display:none;");
        }
	});
	var updateModalValidate = $("#updateModal .form").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
        rules : {
            env : {
                required : true,
                rangelength:[3,50],
                validEnv : true
            },
            title : {
                required : true,
                rangelength:[4, 100]
            },
            order : {
                digits:true
            }
        },
        messages : {
        },
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {

			$.post(base_url + "/task/update",  $("#updateModal .form").serialize(), function(data, status) {
                if (data.status == 200) {
                    $('#addModal').modal('hide');

                    layer.open({
                        icon: '1',
                        content: '更新成功' ,
                        end: function(layero, index){
                            window.location.reload();
                        }
                    });
                } else {
                    layer.open({
                        icon: '2',
                        content: (data.message||'更新失败')
                    });
                }
			});
		}
	});
	$("#updateModal").on('hide.bs.modal', function () {
		$("#updateModal .form")[0].reset();
		addModalValidate.resetForm();
		$("#updateModal .form .form-group").removeClass("has-error");
	});

	
});
